package com.robotserver.orders.common;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * DateFormatUtil
 */
public final class DateFormatUtil {

    private static Logger logger = LoggerFactory.getLogger(DateFormatUtil.class);

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat format2 = new SimpleDateFormat("yyyyMM");
    private static List<String> workList = Arrays.asList("","周日","周一","周二","周三","周四","周五","周六");
    private static SimpleDateFormat formatDay = new SimpleDateFormat("dd");
    private static SimpleDateFormat format4 = new SimpleDateFormat("yyyyMMddHHmm");



    /**
     * @param date 需要格式化的时间
     * @return 时间字符串：格式=yyyy-MM-dd HH:mm:ss
     */
    public static String formatDefault(Date date) {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
    public static String formatDay(Date date) {
        format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(date);
    }

    public static String formatDayStr(Date date) {
        formatDay = new SimpleDateFormat("dd");
        return formatDay.format(date);
    }

    /**
     * @param date 需要格式化的时间
     * @return 时间字符串：格式=yyyyMM
     */
    public static String formatMonth(Date date) {
        format2 = new SimpleDateFormat("yyyyMM");
        return format2.format(date);
    }

    /**
     * @param date 需要格式化的时间
     * @return 时间字符串：格式=HH
     */
    public static String formatToHH(Date date) {
        format = new SimpleDateFormat("HH");
        return format.format(date);
    }

    public static String formatToMM(Date date) {
        format = new SimpleDateFormat("mm");
        return format.format(date);
    }

    public static List<Date> workDayList(int count, Date startDate){
        List<Date> dates = new ArrayList<>();
        Date date = startDate;
        Calendar calendar = Calendar.getInstance();
        int i=1;
        while (i<=count){
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek!=1 && dayOfWeek!=7){
                dates.add(date);
                i++;
            }
            date = DateUtils.addDays(date,1);
        }
        return dates;
    }

    /**
     * @param date 需要格式化的时间
     * @return 时间字符串：格式=yyyy-MM-dd
     */
    public static String formatToyyyy_MM_dd(Date date) {
        format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }


    public static String formatToWork(Date date) {
        format = new SimpleDateFormat("MM月dd日");
        String dateStr = format.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String work = workList.get(dayOfWeek);
        return dateStr +"（"+work+"）";
    }

    /**
     * String日期转date
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date StringToDateFormat(String str)  {
        if (StringUtils.isBlank(str)){
            return null;
        }
        Date date = null;
        try {
            SimpleDateFormat defformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = defformat.parse(str);
        } catch (Exception e) {
            //logger.error(str+":: > StringToDate err.");
        }
        return date;
    }

    /**
     * String日期转date
     * @param
     * @return
     * @throws ParseException
     */
    public static Date StringToDateFormat(String dateStr,String dateFormat)  {
        if (StringUtils.isBlank(dateStr) || "0".equals(dateStr)){
            return null;
        }

        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(dateFormat);
            date = format.parse(dateStr);
        } catch (Exception e) {
            logger.error(dateStr+":: > StringToDate err.");
        }
        return date;
    }

    public static String format(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }

    public static Date StringToDate(String times) {
        if (StringUtils.isBlank(times)){
            return null;
        }
        Date date = null;
        try {
            date = format4.parse(times);
        } catch (Exception e) {
            //logger.error(times+":: > StringToDate err.");
        }
        return date;
    }
}
